package com.sls.nzdapplication.app;

import android.content.Context;
import android.content.SharedPreferences;

import static android.content.Context.MODE_PRIVATE;

/**
 * 这个类是一个工具类，主要是用来保存用户信息和获取用户信息的。其中包括了以下方法：
 *
 * saveUserInfo(String username ,String token,String server)：保存用户信息，包括用户名、token和服务器地址。
 * saveAddress(String address)：保存地址信息。
 * getAddress()：获取地址信息。
 * getUserValue(String type)：获取态信息。
 * clearUserInfo()：清空用户信息。
 * 这个类使用了SharedPreferences来保存数据，其中PREFS_NAME是SharedPreferences的文件名，保存在/data/data/<package name>/shared_prefs/目录下。
 *
 * */
public class MySharedPreferences {
    private Context context;

    private final String PREFS_NAME = "ndzPrefsFile";

    public MySharedPreferences(Context context){
            this.context = context;
    }

    /**
     * 保存用户信息
     */
    public void saveUserInfo(String username ,String token,String server){
        SharedPreferences userInfo = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        SharedPreferences.Editor editor = userInfo.edit();//获取Editor
        //得到Editor后，写入需要保存的数据
        editor.putString("username", username);
        editor.putString("token", token);
        editor.putString("server",server);
        editor.commit();//提交修改
    }


    public void saveAddress(String address){
        SharedPreferences taskid = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        SharedPreferences.Editor editor = taskid.edit();
        editor.putString("address",address);
        editor.commit();
    }

    public String getAddress(){
        SharedPreferences addressInfo = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        return addressInfo.getString("address","");
    }

    //激活任务
    public void saveTaskId(String taskId){
        SharedPreferences taskid = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        SharedPreferences.Editor editor = taskid.edit();
        editor.putString("OpenTaskId",taskId);
        editor.commit();
    }

    public String getOpenTaskId(){
        SharedPreferences taskinfo = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        return taskinfo.getString("OpenTaskId","");
    }

    public void clearOpenTaskId(){
        SharedPreferences taskinfo = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        SharedPreferences.Editor editor = taskinfo.edit();
        editor.remove("OpenTaskId");
        editor.commit();
    }

    //启动任务
    public void saveActiveTaskId(String taskId){
        SharedPreferences taskid = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        SharedPreferences.Editor editor = taskid.edit();
        editor.putString("ActiveTaskId",taskId);
        editor.commit();
    }

    public String getActiveTaskId(){
        SharedPreferences taskinfo = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        return taskinfo.getString("ActiveTaskId","");
    }

    public void clearActiveTaskId(){
        SharedPreferences taskinfo = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        SharedPreferences.Editor editor = taskinfo.edit();
        editor.remove("ActiveTaskId");
        editor.commit();
    }


    public String getUserValue(String type){
        SharedPreferences userInfo = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        return userInfo.getString(type, "");
    }

    /**
     * 清空数据
     */
    public void clearUserInfo(){
        SharedPreferences userInfo = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        SharedPreferences.Editor editor = userInfo.edit();//获取Editor
        editor.remove("username");
        editor.remove("token");
        editor.remove("server");

        editor.commit();

    }






}
